package org.spring.springboot.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.base.ServletUtil;
import org.spring.springboot.domain.Area;
import org.spring.springboot.service.AreaService;
import org.spring.springboot.view.PoiExcelExport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by 赵新国 on 07/02/2017.
 */
@RestController
public class AreaRestController {

    private static final Logger logger = LoggerFactory.getLogger(AreaRestController.class);

    @Autowired
    private AreaService areaService;

    @GetMapping("/api/area")
    public Map<String, Area> findArea() {
        return areaService.find();
    }

    @GetMapping("/api/export")
    public ModelAndView exportArea(Model model) {
        List<Area> areas = areaService.export();
        return new ModelAndView("AreaListExcel", "areas", areas);
    }

    @GetMapping("/api/export2")
    public void exportArea2(HttpServletRequest req, HttpServletResponse resp) {

        long start = System.currentTimeMillis();

        String fileName = "串行-springMVC导出excel小练习.xls";
        ServletUtil su = new ServletUtil(fileName, req, resp);
        su.poiExcelServlet();

        String[] heads = {"省份","市","区、县","区域ID"};
        String[] cols = {"provinceName","cityName","areaName","areaId"};
        int[] numerics = {2};
        List<Area> list = areaService.export();

        ServletUtil suresp = new ServletUtil(resp);
        PoiExcelExport<Area> pee = new PoiExcelExport<Area>(fileName, heads, cols, list, numerics, suresp.getOut());
        pee.exportExcel();

        long end = System.currentTimeMillis();

        logger.info("导出Excel耗时：" + (end -start) + " 毫秒.");
    }

    @GetMapping("/api/export3")
    public void exportArea3(HttpServletRequest req, HttpServletResponse resp) {

        long start = System.currentTimeMillis();

        String fileName = "并行-springMVC导出excel小练习.xls";
        ServletUtil su = new ServletUtil(fileName, req, resp);
        su.poiExcelServlet();

        String[] heads = {"省份","市","区、县","区域ID"};
        String[] cols = {"provinceName","cityName","areaName","areaId"};
        int[] numerics = {2};

        List<Area> list = areaService.export();

        ServletUtil suresp = new ServletUtil(resp);
        PoiExcelExport<Area> pee = new PoiExcelExport<Area>(fileName, heads, cols, list, numerics, suresp.getOut());
        pee.exportExcel();

        long end = System.currentTimeMillis();

        logger.info("导出Excel耗时：" + (end -start) + " 毫秒.");
    }

}
